import React from 'react'

import ReportContent from "../../components/Report";
import {secs2time} from "../../utils/utils";
import moment from "moment";
import {fetchStayReportData} from "../../services/report";

const handleDataFormat = (data, type) => {
  if (!data) return;
  if (type === 'detail') {
    return data.map((item, idx) => {
      const {device_id, start_stamp, end_stamp, duration, lat, lng} = item;
      return {
        key: idx,
        device_id,
        xy: `${parseInt(lat) / 1e6}, ${parseInt(lng) / 1e6}`,
        lat_lng: {lng: lng / 1e6, lat: lat / 1e6},
        duration: secs2time(duration * 1e3),
        start_time: moment(start_stamp).format('YYYY-MM-DD hh:mm:ss'),
        end_time: moment(end_stamp).format('YYYY-MM-DD hh:mm:ss'),
      }
    })
  }
  return data.map((item, idx) => {
    const {
      device_id,
      stay_times,
      duration,
      start_time,
      end_time,
    } = item;

    return {
      key: idx,
      device_id,
      stay_times,
      duration: secs2time(duration * 1e3),
      start_time: moment(start_time).format('YYYY-MM-DD hh:mm:ss'),
      end_time: moment(end_time).format('YYYY-MM-DD hh:mm:ss'),
    }
  })
};

// # 设备编号  停留次数  停留时间 开始时间 结束时间  停留明细


// todo fix
const columnsDay = (handleDetailClick) => ([
  {
    dataIndex: "device_id",
    title: "设备编号"
  },
  {
    dataIndex: "stay_times",
    title: "停留次数"
  },
  {
    dataIndex: "duration",
    title: "停留时间"
  },
  {
    dataIndex: "start_time",
    title: "开始时间"
  },
  {
    dataIndex: "end_time",
    title: "结束时间"
  },
  {
    title: '操作',
    dataIndex: '',
    key: 'x',
    render: (text, record, index) => <a href="javascript:"
                                        onClick={handleDetailClick(text, record)}>明细</a>,
  },
]);


// # 设备编号 停留开始时间  停留结束时间  停留时长 里程 位置

const columnsDetail = (handleTableClick) => ([
  {
    dataIndex: "device_id",
    title: "设备编号"
  },
  {
    dataIndex: "start_time",
    title: "停留开始时间"
  },
  {
    dataIndex: "end_time",
    title: "停留结束时间"
  },
  {
    dataIndex: "duration",
    title: "停留时长"
  },
  {
    dataIndex: "xy",
    title: "位置",
    render: (text, record) => (
      <span>
            <a href="javascript:" onClick={handleTableClick(record.lat_lng)}>{text}</a>
          </span>
    ),
  },
]);

const Stay = (props) => (
  <ReportContent {...props} title="停留报表" fetch={fetchStayReportData} handleDataFormat={handleDataFormat}
                 columnsDay={columnsDay}
                 columnsDetail={columnsDetail}/>);


export default Stay;
